<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use \temmoku\db;
use \admin\home;
use \temmoku\lib\pinyin;
use admin\public_class\article;
use \temmoku\lib\pages;
class special extends home
{
	
	public function category(){
		if($_POST['step']=='post'){
			if(!$_POST['name']){
				$this->err("专题名称不可为空");
			}
			$_POST['sid']=intval($_POST['sid']);
			$array=[
					'name'=>$_POST['name'],
					'upsid'=>$_POST['sid']
				];
			db::insert(jab."special_category")->cols($array)->query();
			$this->category_level();
			$data=$this->category_list();
			\admin\public_class\cache::special_category_setting($data);
			$this->Success('专题分类创建成功',FromUrl);
		}
		$special_list=(new article)->get_special_category();
    	$this->assign('special_list', $special_list);
		$data=$this->category_list();
		$this->assign('data', $data);
		$this->display(tpl('special/category'));
	}
	
	
	private function category_list($sid=0){

    	static $allDB=[];
		$level='';
		$query=db::select('*')->from(jab."special_category")->where("upsid = '$sid'")->orderByDESC(['reorder'])->query();
	    if($query){
	        foreach($query AS $key=>$row){
	        	extract(db::select('count(*) AS NUM')->from(jab."special_list")->where("sid= '$row[sid]' ")->row());
	        	$row['num']=$NUM;
	        	
	        	if($row['upsid']!=0){
	        		$row['level']=$row['level']-1;
		        	$level="&nbsp;&nbsp;&nbsp;|";
			        for($i=0;$i<$row['level'];$i++){
			        	$level.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			        }
		        	$level.="|--";
		        	$row['levels']=$level;
		        	$allDB[] = $row;
		        	$this->category_list($row['sid']);
		        }else{
		        	$row['levels']=$level;
		        	$allDB[] = $row;
		        	$this->category_list($row['sid']);
		        }
			}
	    }
	    return $allDB;
	}
	
	private function category_level($level=0,$sid=0){
		db::update(jab."special_category")->cols(array('level'=>$level+1))->where("upsid='$sid'")->query();
		$categoryDB=db::select('*')->from(jab."special_category")->where("upsid='$sid'")->query();
		foreach($categoryDB AS $row){
			$this->category_level($row['level'],$row['sid']);
		}
	}
	
	public function category_edit(){
		
		if($_POST['step']=='post'){
			$_POST['post']['setting']=serialize($_POST['setting']);
			$_POST['post']['upsid']=(int)$_POST['post']['upsid'];
			db::update(jab."special_category")->cols($_POST['post'])->where("sid='{$_POST['sid']}'")->query();
			$this->category_level();
			$data=$this->category_list();
			\admin\public_class\cache::special_category_setting($data);
			$this->success('修改成功');
		}
		
		$sid=intval($_GET['sid']);
		if(!$sid){
			$this->err('缺少必要参数');
		}
		
		$category_dir=APP_PATH."/home/view/template/".C('select_template')."/pc/special/category";
    	$content_dir=APP_PATH."/home/view/template/".C('select_template')."/pc/special/content";
    	$list_template=$this->Get_tpl($category_dir);
    	$content_template=$this->Get_tpl($content_dir);
		$data=db::select('*')->from(jab."special_category")->where("sid= '$sid' ")->row();
		$special_list=(new article)->get_special_category('0',[$data['upsid']]);
		$this->assign('data', $data);
		$this->assign('list_template', $list_template);
		$this->assign('content_template', $content_template);
		
    	$this->assign('special_list', $special_list);
		$this->display(tpl('special/category_edit'));
	}
	public function batch_category_action(){
    	if($_POST['step']==='reorder'){
    		foreach($_POST['reorder'] as $sid=>$reorder){
    			db::update(jab."special_category")->cols(['reorder'=>$reorder])->where("sid='$sid'")->query();
    		}
    	}elseif($_POST['step']==='pinyin'){
    		foreach($_POST['iddb'] as $key=>$sid){
    			$data=db::select('name')->from(jab."special_category")->where("sid= '$sid' ")->row();
    			$pinyin= pinyin::Pinyin($data['name']);
    			db::update(jab."special_category")->cols(['dir_name'=>$pinyin])->where("sid='$sid'")->query();
    		}
    	}
    	$this->Success('操作成功');
    }
    /*删除分类操作*/
    public function category_del(){
    	$sid=intval($_GET['sid']);
    	$data=db::select('*')->from(jab."special_category")->where("sid= '$sid' ")->row();
    	if(!$data){
    		$this->err('数据不存在，无需删除');
    	}
    	$datason=db::select('*')->from(jab."special_category")->where("upsid= '$sid' ")->row();
    	if($datason){
    		$this->err('请先删除子类');
    	}
    	$data_list=db::select('id')->from(jab."special_list")->where("sid='$sid'")->query();
    	
    	foreach($data_list AS $row){
    		//进行文章的删除
    		db::delete(jab.'special_article')->where("lid='{$row['id']}'")->query();
    		//进行专题的删除
    		db::delete(jab.'special_list')->where("id='{$row['id']}'")->query();
    	}
    	//进行分类的删除
    	db::delete(jab.'special_category')->where("sid='$sid'")->query();
    	$data=$this->category_list();
		\admin\public_class\cache::special_category_setting($data);
    	$this->success('删除成功');
    }
    
    
    public function edit()
    {
    	if('post'===$_POST['step']){
    		if(!$_POST['id']){
    			$this->err('缺少必要参数ID');
    		}
    		$data=db::select('*')->from(jab."special_list")->where("id= '$_POST[id]' ")->row();
    		$thumb=$data['thumb'];
    		if($_POST['post']['thumb']){
    			if($thumb != $_POST['post']['thumb']){
    				$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$thumb;
		    		//删除备份文件
					del_file($oldthumb);
    			}
    			$_POST['post']['thumb']=UploadFiles_MOVE($_POST['post']['thumb'],"/special/$data[upsid]",TRUE);
    		}elseif($thumb){
    			$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$thumb;
	    		//删除备份文件
				del_file($oldthumb);
    		}
    		$_POST['post']['setting']=serialize($_POST['setting']);
    		
    		if(!$_POST['post']['seo_description'] && $_POST['post']['content']){
    			$_POST['post']['seo_description']=Get_word_strpos($_POST['post']['content'],'200');
    		}
			$_POST['post']['sid']=(int)$_POST['post']['sid'];
    		db::update(jab."special_list")->cols($_POST['post'])->where('id='.$_POST['id'])->query();
    		$get_all_special_list=(new Article)->get_all_special_list();
    		\admin\public_class\cache::special_list_setting($get_all_special_list);
	    	$this->Success('专题修改成功',FromUrl);
    	}
    	
    	$id=intval($_GET['id']);
    	$content_dir=APP_PATH."/home/view/template/".C('select_template')."/pc/special/content";
		$content_template=$this->Get_tpl($content_dir);
    	$data=db::select('*')->from(jab."special_list")->where("id= '$id'")->row();
		$this->assign('content_template', $content_template);
    	$special_category=(new Article)->get_special_category('0',[$data['sid']]);
    	$this->assign('special_category', $special_category);
    	$this->assign('data', $data);
    	$this->display(tpl('special/edit'));
    }
    
    public function del()
    {
    	//进行文章的删除
    	db::delete(jab.'special_article')->where("lid='$_GET[id]'")->query();
    	//进行专题的删除
    	db::delete(jab.'special_list')->where("id='$_GET[id]'")->query();
    	$get_all_special_list=(new Article)->get_all_special_list();
    	\admin\public_class\cache::special_list_setting($get_all_special_list);
    	$this->success('专题删除成功');
    }
    
    public function add(){
    	if($_POST['step']=='add'){
    		if(!$_POST['name']){
    			$this->err('专题名称不可为空');
    		}
    		$sid=intval($_POST['sid']);
    		if(!$sid){
    			$this->err('请选择一个要添加的分类');
    		}
    		$array=['name'=>$_POST['name'],'sid'=>$sid,'posttime'=>NOWTIME];
    		db::insert(jab."special_list")->cols($array)->query();
    		$get_all_special_list=(new Article)->get_all_special_list();
    		\admin\public_class\cache::special_list_setting($get_all_special_list);
    		
    		$this->success('专题添加成功');
    	}else{
    		$this->err('缺少必要参数');
    	}
    }
    
    public function index()
    {	
    	$_GET['sid'] ? $where="sid='$_GET[sid]'" : $where = "1";
    	!isset($_GET['page']) && $_GET['page']=1;
    	$rows='20';
    	
    	$_data=db::select('*')->from(jab."special_list")->where($where)->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		
		foreach($_data AS $row){
			extract(db::select('count(*) AS article_num')->from(jab."special_article")->where("lid= '$row[id]' ")->row());
			$row['article_num']=$article_num;
			extract(db::select('count(*) AS comment_num')->from(jab."special_comment")->where("lid= '$row[id]' ")->row());
			$row['comment_num']=$comment_num;
			$data[]=$row;
		}
		
		
		$Get_Page=(new pages)->Pages(jab."special_list",$where,"/admin/special/index/",$rows,$_GET['page']);
		$special_category=(new article)->get_special_category(0,[$_GET['sid']]);
    	$this->assign('special_category', $special_category);
    	$this->assign('data', $data);
    	$this->assign('Get_Page', $Get_Page);
    	$this->display(tpl('special/index'));
    }
    
    
	public function articles(){
		if(!$_GET['id']){
			$this->err("缺少ID");
		}
		$special=db::select('*')->from(jab."special_list")->where("id= '$_GET[id]'")->row();
		
		if(!$special){
			$this->err('不存在的数据');
		}
		$this->assign('special', $special);
    	!isset($_GET['page']) && $_GET['page']=1;
    	$rows='100';
    	$where="lid=$_GET[id]";
    	$_data=db::select('*')->from(jab."special_article")->where($where)->orderByDESC(['reorder'])->setPaging($rows)->page($_GET['page'])->query();
		foreach($_data AS $row){
			$_row=db::select('*')->from(jab."article")->where("aid= '$row[aid]' ")->row();
			$_row['id']=$row['id'];
			$_row['reorder']=$row['reorder'];
			$data[]=$_row;
		}
		$this->assign('data', $data);
		$Get_Page=(new pages)->Pages(jab."special_article",$where,"/admin/special/articles/id_$_GET[id]/",$rows,$_GET['page']);
		$category = (new article)-> allcidDB('0',jab.'category');
        $this->assign('Get_Page', $Get_Page);
        
        $this->assign('category', $category);
		
		
		$this->display(tpl('special/articles'));
	}
	
	public function batch_article_action(){
		if($_POST['step']=='reorder'){
			foreach($_POST['reorder'] AS $id=>$reorder){
				$array=['reorder'=>$reorder];
				db::update(jab."special_article")->cols($array)->where("id='$id'")->query();
			}
			$this->success('排序完成');
		}elseif($_POST['step']=='del'){
			if(!$_POST['iddb']){
				$this->err('最少选择一片文章进行删除');
			}
			$iddb=implode(",",$_POST['iddb']);
			db::delete(jab."special_article")->where("id IN($iddb)")->query();
			$this->success('删除成功');
		}else{
			$this->err("缺少必要参数或者方法不正确");
		}
	}
	
	public function articls_del(){
		db::delete(jab."special_article")->where("id='$_GET[id]'")->query();
		$this->success('删除成功');
	}
	
	public function ajax_article(){
		$rows='50';
    	$where="1";
    	$article_in=db::select('aid')->from(jab."special_article")->where("lid='$_POST[id]'")->query();
    	if($article_in){
	    	foreach($article_in AS $row){
	    		$data[]=$row['aid'];
	    	}
	    	$notin= implode(',',$data);
	    	unset($data);
    		$where .=" AND aid NOT IN($notin)";
    	}
    	
    	!isset($_POST['page']) && $_POST['page']=1;
    	
    	if($_POST['cid']){
    		$where .=" AND cid=$_POST[cid]";
    	}
    	if($_POST['keyword']){
    		$where .=" AND title LIKE '%{$_POST['keyword']}%'";
    	}
    	
    	$_data=db::select('aid,cid,title')->from(jab."article")->where($where)->orderByDESC(['aid'])->setPaging($rows)->page($_POST['page'])->query();
		extract(db::select('count(*) AS NUM')->from(jab."article")->where($where)->row());
		foreach($_data AS $row){
			$row['url']=U(['aid'=>$row['aid'],'cid'=>$row['cid']],'home');
			$data[]=$row;
		}
		$array=['code'=>'1','text'=>'操作成功','data'=>$data,'page'=>$_POST['page'],'rows'=>$rows,'count'=>$NUM];
		echo json_encode($array);
	}
	
	public function ajax_add_article(){
		$is_article=db::select('id')->from(jab."special_article")->where("lid=$_POST[id] AND aid=$_POST[aid]")->row();
		if(!$is_article){
			$array=['lid'=>$_POST['id'],'aid'=>$_POST['aid'],'posttime'=>NOWTIME,'username'=>C('mydb.username')];
			$id=db::insert(jab."special_article")->cols($array)->query();
		}
		!isset($id) && $id=$is_article['id'];
		$array=['code'=>'1','text'=>'添加完成','id'=>$id];
		echo json_encode($array);
	}
	
	public function comment(){
		if(!$_GET['id']){
			$this->err("缺少ID");
		}
		$special=db::select('*')->from(jab."special_list")->where("id= '$_GET[id]'")->row();
		if(!$special){
			$this->err('不存在的数据');
		}
		$this->assign('special', $special);
		
		$rows='100';
    	$where="lid=$_GET[id]";
    	
    	if($_GET['state']){
    		$where .=" AND state=$_GET[state]";
    	}
    	
    	!isset($_GET['page']) && $_GET['page']=1;
    	$data=db::select('*')->from(jab."special_comment")->where($where)->orderByDESC(['id'])->setPaging($rows)->page($_GET['page'])->query();
		$this->assign('data', $data);
		$Get_Page=(new pages)->Pages(jab."special_comment",$where,"/admin/special/comment/id_$_GET[id]/",$rows,$_GET['page']);
		$this->assign('Get_Page', $Get_Page);
		
		$this->display(tpl('special/comment'));
	}
    
    public function comment_action(){
    	if($_POST['id']){
    		if($_POST['step']=='state'){
    			db::update(jab."special_comment")->cols(['state'=>$_POST['state']])->where('id='.$_POST['id'])->query();
    		}elseif($_POST['step']=='del'){
    			db::delete(jab."special_comment")->where('id='.$_POST['id'])->query();
    		}
    		$array=['code'=>1,"text"=>"操作成功"];
    		echo json_encode($array);
    	}else if($_POST['iddb']){
    		$iddb=implode(',',$_POST['iddb']);
    		if($_POST['step']=='state'){
    			db::update(jab."special_comment")->cols(['state'=>$_POST['state']])->where('id IN('.$iddb.')')->query();
    		}elseif($_POST['step']=='del'){
    			db::delete(jab."special_comment")->where('id IN('.$iddb.')')->query();
    		}
    		$this->success('操作成功');
    	}
    	
    }
    
    
    private function Get_tpl($dir){
		$data=[];
		if ( $handle  =  @opendir ( $dir )) {
			while (( $file  =  readdir ( $handle ))!== false) {
				if($file==='.' || $file==='..'){
					continue;
				}
				$files=$dir."/".$file;
				if(is_file($files)){
					$php=strtolower(strrchr($files,"."));
					if($php=='.php'){
						$data[]=@include_once $files;
					}
				}
			}
			//关闭打开的目录
			closedir ( $handle );
		}
		return $data;
	}
}
?>